package org.pk.preferences;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import com.cs.pk.utils.FontUtil;

public class BuildInformation extends PreferencePage implements
		IWorkbenchPreferencePage, SelectionListener {

	IWorkbench workbench = null;
	Composite preference = null;
	Font tableFont = null;

	protected Control createContents(Composite parent) {
		preference = parent;
		noDefaultAndApplyButton();
		return displayBuildItems(parent);
	}

	public void init(IWorkbench workbench) {
		this.workbench = workbench;
	}

	public void dispose() {
		super.dispose();
		if (tableFont != null) {
			tableFont.dispose();
			tableFont = null;
		}
	}

	private Control displayBuildItems(Composite parent) {
		Composite buildPlane = new Composite(parent, SWT.NULL);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = false;
		buildPlane.setLayoutData(data);
		buildPlane.setLayout(new GridLayout());

		Label info = new Label(buildPlane, SWT.SHADOW_IN | SWT.LEFT | SWT.WRAP);
		info
				.setText("Here you can create/update/remove your build information.");

		Composite buildItems = new Composite(buildPlane, SWT.NULL | SWT.BORDER);
		data = new GridData(GridData.FILL_BOTH);
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = true;
		buildItems.setLayoutData(data);
		buildItems.setLayout(new GridLayout(2, false));

		final Table table = new Table(buildItems, SWT.BORDER | SWT.V_SCROLL
				| SWT.H_SCROLL|SWT.FULL_SELECTION);
		data = new GridData(GridData.FILL_BOTH);
		table.setLayoutData(data);
		tableFont = FontUtil.adjustFont(table, 10, SWT.BOLD);
		populateTable(table);

		Composite itemList = new Composite(buildItems, SWT.NULL);
		data = new GridData(GridData.FILL_VERTICAL);
		data.grabExcessHorizontalSpace = false;
		data.grabExcessVerticalSpace = false;
		itemList.setLayoutData(data);
		itemList.setLayout(new GridLayout());

		createButton("Add..", new AddCmd(table), itemList);
		final Button remove = createButton("Remove", new RemoveCmd(table), itemList);
		remove.setEnabled(false);
		final Button update = createButton("Update", new UpdateCmd(table), itemList);
		update.setEnabled(false);
		
		table.addSelectionListener(new SelectionListener(){

			public void widgetDefaultSelected(SelectionEvent e) {}

			public void widgetSelected(SelectionEvent e) {
				System.out.println("prabhatk: "+this.getClass().getName()+" "+e.getSource());
				Table table = (Table)e.getSource();
				System.out.println("prabhatk: "+this.getClass().getName()+" selected index "+table.getSelectionIndex());
				if(table.getSelectionIndex()>-1){
					 remove.setEnabled(true);
					 update.setEnabled(true);
				}else{
					remove.setEnabled(false);
					update.setEnabled(false);
				}
			}
			
		});

		return buildPlane;

	}

	private Button createButton(String value, Object command, Composite parent) {
		Button actionButton = new Button(parent, SWT.PUSH);
		actionButton.setText(value);
		actionButton.addSelectionListener(this);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 1;
		actionButton.setLayoutData(data);
		actionButton.setData(command);
		return actionButton;
	}

	public void widgetDefaultSelected(SelectionEvent e) {

	}

	public void widgetSelected(SelectionEvent e) {
		Button action = (Button) e.getSource();
		((Command) action.getData()).execute();
	}

	private interface Command {
		public void execute();
	}

	private abstract class CmdAdapter implements Command {
		protected Table buildTable = null;

		public CmdAdapter(Table table) {
			buildTable = table;
		}

		public void displayForm() {

//			BuildForm bf = new BuildForm(preference.getShell(), buildTable,
//					getBuildName());
//			bf.showForm();
		}

		protected abstract String getBuildName();

	}

	private class AddCmd extends CmdAdapter {

		public AddCmd(Table table) {
			super(table);
		}

		public void execute() {
			displayForm();
		}

		public String getBuildName() {
			return null;
		}
	}

	private class RemoveCmd extends CmdAdapter {

		public RemoveCmd(Table table) {
			super(table);
		}

		public void execute() {
//			Activator.getDefault().removeBuildItem(getBuildName());
//			Util.setToDefaultValue(generateKey(Constants.BUILD_ROOT_DIR));
//			Util.setToDefaultValue(generateKey(Constants.BUILD_ENV_VARIABLE));//,nameTxt.getText().trim()), enviornmentTxt
//			Util.setToDefaultValue(generateKey(Constants.BUILD_COMMAND));//,nameTxt.getText().trim()), commandTxt
//			Util.setToDefaultValue(generateKey(Constants.BUILD_COMPLETE_CMD));//,nameTxt.getText().trim()), buildCmdTxt
//			Activator.getDefault().savePluginPreferences();
//			int selectedIndex = buildTable.getSelectionIndex();
//			buildTable.remove(selectedIndex);
		}
		
		private String generateKey(String prefix) {
			return prefix + "." + getBuildName();
		}

		public String getBuildName() {
			int selectedIndex = buildTable.getSelectionIndex();
			return (String) buildTable.getItem(selectedIndex).getData();
		}
	}

	private class UpdateCmd extends CmdAdapter {

		public UpdateCmd(Table table) {
			super(table);
		}

		public void execute() {
			displayForm();
		}

		public String getBuildName() {
			int selectedIndex = buildTable.getSelectionIndex();
			return (String) buildTable.getItem(selectedIndex).getData();
		}
	}

	private void populateTable(Table table) {
//			for (Iterator iterator = Activator.getDefault().buildItems.iterator(); iterator.hasNext();) {
//				String name = (String) iterator.next();
//				TableItem item = new TableItem(table, 0);
//				item.setText(name);
//				item.setData(name);
//			}
	}

}
